﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace homework9
{
    public partial class Form : System.Windows.Forms.Form
    {
        private bool init = false;
       
        public Form()
        {
            InitializeComponent();
            PutinData();        
        }
       
       
        public List<string> ReadAllChinese()
        {
            List<string> chineseList = new List<string>();
            SQLiteConnection connection = null;
            try
            {
                string dataConnPath = @"Data Source =./database.db";              
                connection = new SQLiteConnection(dataConnPath);
                connection.Open();            
                SQLiteCommand command = new SQLiteCommand(@"SELECT chinese FROM words", connection);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        chineseList.Add(reader.GetString(0));
                    }
                }
                connection.Close();
            }
            catch (Exception exception)
            {
                if (connection != null)
                {
                    connection.Close();
                }
                MessageBox.Show(exception.Message);
            }
            return chineseList;
        }


        private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!init) return;
            int rowIndex = e.RowIndex;
            DataGridViewRow row = dataGridView.Rows[rowIndex];
            string chinese = row.Cells[0].Value.ToString();
            string answer = ReadEnglish(chinese);
            if (answer.Equals(row.Cells[1].Value.ToString()))
            {
                row.Cells[2].Value = "你答对了";
            }
            else
            {
                row.Cells[2].Value = "答案错误，为" + "(" + answer + ")";
            }
        }
        public string ReadEnglish(string chinese)
        {
            string english = "";
            SQLiteConnection connection = null;
            try
            {
                string dataConnPath = @"Data Source =./database.db";
                connection = new SQLiteConnection(dataConnPath);
                connection.Open();

                SQLiteCommand command = new SQLiteCommand(@"SELECT english FROM words WHERE chinese = $chinese", connection);
                command.Parameters.AddWithValue("chinese", chinese);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        english = reader.GetString(0);
                        break;
                    }
                }
                connection.Close();
            }
            catch (Exception exception)
            {
                if (connection != null)
                {
                    connection.Close();
                }
                MessageBox.Show(exception.Message);
            }
            return english;
        }
        public void PutinData()
        {
            List<string> chineseList = ReadAllChinese();
            chineseList.ForEach(chinese =>
            {
                string[] row = { chinese, "", "" };
                dataGridView.Rows.Add(row);
            });
            init = true;
        }
    }
}
